<?php

/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
/* geoip.inc
 *
 * Copyright (C) 2007 MaxMind LLC
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

define("GEOIP_COUNTRY_BEGIN", 16776960);
define("GEOIP_STATE_BEGIN_REV0", 16700000);
define("GEOIP_STATE_BEGIN_REV1", 16000000);
define("GEOIP_STANDARD", 0);
define("GEOIP_MEMORY_CACHE", 1);
define("GEOIP_SHARED_MEMORY", 2);
define("STRUCTURE_INFO_MAX_SIZE", 20);
define("DATABASE_INFO_MAX_SIZE", 100);
define("GEOIP_COUNTRY_EDITION", 106);
define("GEOIP_PROXY_EDITION", 8);
define("GEOIP_ASNUM_EDITION", 9);
define("GEOIP_NETSPEED_EDITION", 10);
define("GEOIP_REGION_EDITION_REV0", 112);
define("GEOIP_REGION_EDITION_REV1", 3);
define("GEOIP_CITY_EDITION_REV0", 111);
define("GEOIP_CITY_EDITION_REV1", 2);
define("GEOIP_ORG_EDITION", 110);
define("GEOIP_ISP_EDITION", 4);
define("SEGMENT_RECORD_LENGTH", 3);
define("STANDARD_RECORD_LENGTH", 3);
define("ORG_RECORD_LENGTH", 4);
define("MAX_RECORD_LENGTH", 4);
define("MAX_ORG_RECORD_LENGTH", 300);
define("GEOIP_SHM_KEY", 0x4f415401);
define("US_OFFSET", 1);
define("CANADA_OFFSET", 677);
define("WORLD_OFFSET", 1353);
define("FIPS_RANGE", 360);
define("GEOIP_UNKNOWN_SPEED", 0);
define("GEOIP_DIALUP_SPEED", 1);
define("GEOIP_CABLEDSL_SPEED", 2);
define("GEOIP_CORPORATE_SPEED", 3);
define("GEOIP_DOMAIN_EDITION", 11);
define("GEOIP_LOCATIONA_EDITION", 13);
define("GEOIP_ACCURACYRADIUS_EDITION", 14);
define("GEOIP_CITYCOMBINED_EDITION", 15);
define("CITYCOMBINED_FIXED_RECORD", 7 );

class GeoIP {
    var $flags;
    var $filehandle;
    var $memory_buffer;
    var $databaseType;
    var $databaseSegments;
    var $record_length;
    var $shmid;
    var $GEOIP_COUNTRY_CODE_TO_NUMBER = array(
"" => 0, "AP" => 1, "EU" => 2, "AD" => 3, "AE" => 4, "AF" => 5, 
"AG" => 6, "AI" => 7, "AL" => 8, "AM" => 9, "AN" => 10, "AO" => 11, 
"AQ" => 12, "AR" => 13, "AS" => 14, "AT" => 15, "AU" => 16, "AW" => 17, 
"AZ" => 18, "BA" => 19, "BB" => 20, "BD" => 21, "BE" => 22, "BF" => 23, 
"BG" => 24, "BH" => 25, "BI" => 26, "BJ" => 27, "BM" => 28, "BN" => 29, 
"BO" => 30, "BR" => 31, "BS" => 32, "BT" => 33, "BV" => 34, "BW" => 35, 
"BY" => 36, "BZ" => 37, "CA" => 38, "CC" => 39, "CD" => 40, "CF" => 41, 
"CG" => 42, "CH" => 43, "CI" => 44, "CK" => 45, "CL" => 46, "CM" => 47, 
"CN" => 48, "CO" => 49, "CR" => 50, "CU" => 51, "CV" => 52, "CX" => 53, 
"CY" => 54, "CZ" => 55, "DE" => 56, "DJ" => 57, "DK" => 58, "DM" => 59, 
"DO" => 60, "DZ" => 61, "EC" => 62, "EE" => 63, "EG" => 64, "EH" => 65, 
"ER" => 66, "ES" => 67, "ET" => 68, "FI" => 69, "FJ" => 70, "FK" => 71, 
"FM" => 72, "FO" => 73, "FR" => 74, "FX" => 75, "GA" => 76, "GB" => 77,
"GD" => 78, "GE" => 79, "GF" => 80, "GH" => 81, "GI" => 82, "GL" => 83, 
"GM" => 84, "GN" => 85, "GP" => 86, "GQ" => 87, "GR" => 88, "GS" => 89, 
"GT" => 90, "GU" => 91, "GW" => 92, "GY" => 93, "HK" => 94, "HM" => 95, 
"HN" => 96, "HR" => 97, "HT" => 98, "HU" => 99, "ID" => 100, "IE" => 101, 
"IL" => 102, "IN" => 103, "IO" => 104, "IQ" => 105, "IR" => 106, "IS" => 107, 
"IT" => 108, "JM" => 109, "JO" => 110, "JP" => 111, "KE" => 112, "KG" => 113, 
"KH" => 114, "KI" => 115, "KM" => 116, "KN" => 117, "KP" => 118, "KR" => 119, 
"KW" => 120, "KY" => 121, "KZ" => 122, "LA" => 123, "LB" => 124, "LC" => 125, 
"LI" => 126, "LK" => 127, "LR" => 128, "LS" => 129, "LT" => 130, "LU" => 131, 
"LV" => 132, "LY" => 133, "MA" => 134, "MC" => 135, "MD" => 136, "MG" => 137, 
"MH" => 138, "MK" => 139, "ML" => 140, "MM" => 141, "MN" => 142, "MO" => 143, 
"MP" => 144, "MQ" => 145, "MR" => 146, "MS" => 147, "MT" => 148, "MU" => 149, 
"MV" => 150, "MW" => 151, "MX" => 152, "MY" => 153, "MZ" => 154, "NA" => 155,
"NC" => 156, "NE" => 157, "NF" => 158, "NG" => 159, "NI" => 160, "NL" => 161, 
"NO" => 162, "NP" => 163, "NR" => 164, "NU" => 165, "NZ" => 166, "OM" => 167, 
"PA" => 168, "PE" => 169, "PF" => 170, "PG" => 171, "PH" => 172, "PK" => 173, 
"PL" => 174, "PM" => 175, "PN" => 176, "PR" => 177, "PS" => 178, "PT" => 179, 
"PW" => 180, "PY" => 181, "QA" => 182, "RE" => 183, "RO" => 184, "RU" => 185, 
"RW" => 186, "SA" => 187, "SB" => 188, "SC" => 189, "SD" => 190, "SE" => 191, 
"SG" => 192, "SH" => 193, "SI" => 194, "SJ" => 195, "SK" => 196, "SL" => 197, 
"SM" => 198, "SN" => 199, "SO" => 200, "SR" => 201, "ST" => 202, "SV" => 203, 
"SY" => 204, "SZ" => 205, "TC" => 206, "TD" => 207, "TF" => 208, "TG" => 209, 
"TH" => 210, "TJ" => 211, "TK" => 212, "TM" => 213, "TN" => 214, "TO" => 215, 
"TL" => 216, "TR" => 217, "TT" => 218, "TV" => 219, "TW" => 220, "TZ" => 221, 
"UA" => 222, "UG" => 223, "UM" => 224, "US" => 225, "UY" => 226, "UZ" => 227, 
"VA" => 228, "VC" => 229, "VE" => 230, "VG" => 231, "VI" => 232, "VN" => 233,
"VU" => 234, "WF" => 235, "WS" => 236, "YE" => 237, "YT" => 238, "RS" => 239, 
"ZA" => 240, "ZM" => 241, "ME" => 242, "ZW" => 243, "A1" => 244, "A2" => 245, 
"O1" => 246, "AX" => 247, "GG" => 248, "IM" => 249, "JE" => 250, "BL" => 251,
"MF" => 252
);
    var $GEOIP_COUNTRY_CODES = array(
"", "AP", "EU", "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN", "AO", "AQ",
"AR", "AS", "AT", "AU", "AW", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH",
"BI", "BJ", "BM", "BN", "BO", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA",
"CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU",
"CV", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG",
"EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "FX", "GA", "GB",
"GD", "GE", "GF", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT",
"GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IN",
"IO", "IQ", "IR", "IS", "IT", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM",
"KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS",
"LT", "LU", "LV", "LY", "MA", "MC", "MD", "MG", "MH", "MK", "ML", "MM", "MN",
"MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA",
"NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA",
"PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY",
"QA", "RE", "RO", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI",
"SJ", "SK", "SL", "SM", "SN", "SO", "SR", "ST", "SV", "SY", "SZ", "TC", "TD",
"TF", "TG", "TH", "TJ", "TK", "TM", "TN", "TO", "TL", "TR", "TT", "TV", "TW",
"TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN",
"VU", "WF", "WS", "YE", "YT", "RS", "ZA", "ZM", "ME", "ZW", "A1", "A2", "O1",
"AX", "GG", "IM", "JE", "BL", "MF"
);
    var $GEOIP_COUNTRY_CODES3 = array(
"","AP","EU","AND","ARE","AFG","ATG","AIA","ALB","ARM","ANT","AGO","ATA","ARG",
"ASM","AUT","AUS","ABW","AZE","BIH","BRB","BGD","BEL","BFA","BGR","BHR","BDI",
"BEN","BMU","BRN","BOL","BRA","BHS","BTN","BVT","BWA","BLR","BLZ","CAN","CCK",
"COD","CAF","COG","CHE","CIV","COK","CHL","CMR","CHN","COL","CRI","CUB","CPV",
"CXR","CYP","CZE","DEU","DJI","DNK","DMA","DOM","DZA","ECU","EST","EGY","ESH",
"ERI","ESP","ETH","FIN","FJI","FLK","FSM","FRO","FRA","FX","GAB","GBR","GRD",
"GEO","GUF","GHA","GIB","GRL","GMB","GIN","GLP","GNQ","GRC","SGS","GTM","GUM",
"GNB","GUY","HKG","HMD","HND","HRV","HTI","HUN","IDN","IRL","ISR","IND","IOT",
"IRQ","IRN","ISL","ITA","JAM","JOR","JPN","KEN","KGZ","KHM","KIR","COM","KNA",
"PRK","KOR","KWT","CYM","KAZ","LAO","LBN","LCA","LIE","LKA","LBR","LSO","LTU",
"LUX","LVA","LBY","MAR","MCO","MDA","MDG","MHL","MKD","MLI","MMR","MNG","MAC",
"MNP","MTQ","MRT","MSR","MLT","MUS","MDV","MWI","MEX","MYS","MOZ","NAM","NCL",
"NER","NFK","NGA","NIC","NLD","NOR","NPL","NRU","NIU","NZL","OMN","PAN","PER",
"PYF","PNG","PHL","PAK","POL","SPM","PCN","PRI","PSE","PRT","PLW","PRY","QAT",
"REU","ROU","RUS","RWA","SAU","SLB","SYC","SDN","SWE","SGP","SHN","SVN","SJM",
"SVK","SLE","SMR","SEN","SOM","SUR","STP","SLV","SYR","SWZ","TCA","TCD","ATF",
"TGO","THA","TJK","TKL","TLS","TKM","TUN","TON","TUR","TTO","TUV","TWN","TZA",
"UKR","UGA","UMI","USA","URY","UZB","VAT","VCT","VEN","VGB","VIR","VNM","VUT",
"WLF","WSM","YEM","MYT","SRB","ZAF","ZMB","MNE","ZWE","A1","A2","O1",
"ALA","GGY","IMN","JEY","BLM","MAF"
    );
    var $GEOIP_COUNTRY_NAMES = array(
"", "Asia/Pacific Region", "Europe", "Andorra", "United Arab Emirates",
"Afghanistan", "Antigua and Barbuda", "Anguilla", "Albania", "Armenia",
"Netherlands Antilles", "Angola", "Antarctica", "Argentina", "American Samoa",
"Austria", "Australia", "Aruba", "Azerbaijan", "Bosnia and Herzegovina",
"Barbados", "Bangladesh", "Belgium", "Burkina Faso", "Bulgaria", "Bahrain",
"Burundi", "Benin", "Bermuda", "Brunei Darussalam", "Bolivia", "Brazil",
"Bahamas", "Bhutan", "Bouvet Island", "Botswana", "Belarus", "Belize",
"Canada", "Cocos (Keeling) Islands", "Congo, The Democratic Republic of the",
"Central African Republic", "Congo", "Switzerland", "Cote D'Ivoire", "Cook Islands",
"Chile", "Cameroon", "China", "Colombia", "Costa Rica", "Cuba", "Cape Verde",
"Christmas Island", "Cyprus", "Czech Republic", "Germany", "Djibouti",
"Denmark", "Dominica", "Dominican Republic", "Algeria", "Ecuador", "Estonia",
"Egypt", "Western Sahara", "Eritrea", "Spain", "Ethiopia", "Finland", "Fiji",
"Falkland Islands (Malvinas)", "Micronesia, Federated States of", "Faroe Islands",
"France", "France, Metropolitan", "Gabon", "United Kingdom",
"Grenada", "Georgia", "French Guiana", "Ghana", "Gibraltar", "Greenland",
"Gambia", "Guinea", "Guadeloupe", "Equatorial Guinea", "Greece", "South Georgia and the South Sandwich Islands",
"Guatemala", "Guam", "Guinea-Bissau",
"Guyana", "Hong Kong", "Heard Island and McDonald Islands", "Honduras",
"Croatia", "Haiti", "Hungary", "Indonesia", "Ireland", "Israel", "India",
"British Indian Ocean Territory", "Iraq", "Iran, Islamic Republic of",
"Iceland", "Italy", "Jamaica", "Jordan", "Japan", "Kenya", "Kyrgyzstan",
"Cambodia", "Kiribati", "Comoros", "Saint Kitts and Nevis", "Korea, Democratic People's Republic of",
"Korea, Republic of", "Kuwait", "Cayman Islands",
"Kazakhstan", "Lao People's Democratic Republic", "Lebanon", "Saint Lucia",
"Liechtenstein", "Sri Lanka", "Liberia", "Lesotho", "Lithuania", "Luxembourg",
"Latvia", "Libyan Arab Jamahiriya", "Morocco", "Monaco", "Moldova, Republic of",
"Madagascar", "Marshall Islands", "Macedonia",
"Mali", "Myanmar", "Mongolia", "Macau", "Northern Mariana Islands",
"Martinique", "Mauritania", "Montserrat", "Malta", "Mauritius", "Maldives",
"Malawi", "Mexico", "Malaysia", "Mozambique", "Namibia", "New Caledonia",
"Niger", "Norfolk Island", "Nigeria", "Nicaragua", "Netherlands", "Norway",
"Nepal", "Nauru", "Niue", "New Zealand", "Oman", "Panama", "Peru", "French Polynesia",
"Papua New Guinea", "Philippines", "Pakistan", "Poland", "Saint Pierre and Miquelon",
"Pitcairn Islands", "Puerto Rico", "Palestinian Territory",
"Portugal", "Palau", "Paraguay", "Qatar", "Reunion", "Romania",
"Russian Federation", "Rwanda", "Saudi Arabia", "Solomon Islands",
"Seychelles", "Sudan", "Sweden", "Singapore", "Saint Helena", "Slovenia",
"Svalbard and Jan Mayen", "Slovakia", "Sierra Leone", "San Marino", "Senegal",
"Somalia", "Suriname", "Sao Tome and Principe", "El Salvador", "Syrian Arab Republic",
"Swaziland", "Turks and Caicos Islands", "Chad", "French Southern Territories",
"Togo", "Thailand", "Tajikistan", "Tokelau", "Turkmenistan",
"Tunisia", "Tonga", "Timor-Leste", "Turkey", "Trinidad and Tobago", "Tuvalu",
"Taiwan", "Tanzania, United Republic of", "Ukraine",
"Uganda", "United States Minor Outlying Islands", "United States", "Uruguay",
"Uzbekistan", "Holy See (Vatican City State)", "Saint Vincent and the Grenadines",
"Venezuela", "Virgin Islands, British", "Virgin Islands, U.S.",
"Vietnam", "Vanuatu", "Wallis and Futuna", "Samoa", "Yemen", "Mayotte",
"Serbia", "South Africa", "Zambia", "Montenegro", "Zimbabwe",
"Anonymous Proxy","Satellite Provider","Other",
"Aland Islands","Guernsey","Isle of Man","Jersey","Saint Barthelemy","Saint Martin"
);

    var $GEOIP_CONTINENT_CODES = array(
  "--", "AS", "EU", "EU", "AS", "AS", "NA", "NA", "EU", "AS", 
  "NA", "AF", "AN", "SA", "OC", "EU", "OC", "NA", "AS", "EU", 
  "NA", "AS", "EU", "AF", "EU", "AS", "AF", "AF", "NA", "AS", 
  "SA", "SA", "NA", "AS", "AN", "AF", "EU", "NA", "NA", "AS", 
  "AF", "AF", "AF", "EU", "AF", "OC", "SA", "AF", "AS", "SA", 
  "NA", "NA", "AF", "AS", "AS", "EU", "EU", "AF", "EU", "NA", 
  "NA", "AF", "SA", "EU", "AF", "AF", "AF", "EU", "AF", "EU", 
  "OC", "SA", "OC", "EU", "EU", "EU", "AF", "EU", "NA", "AS", 
  "SA", "AF", "EU", "NA", "AF", "AF", "NA", "AF", "EU", "AN", 
  "NA", "OC", "AF", "SA", "AS", "AN", "NA", "EU", "NA", "EU", 
  "AS", "EU", "AS", "AS", "AS", "AS", "AS", "EU", "EU", "NA", 
  "AS", "AS", "AF", "AS", "AS", "OC", "AF", "NA", "AS", "AS", 
  "AS", "NA", "AS", "AS", "AS", "NA", "EU", "AS", "AF", "AF", 
  "EU", "EU", "EU", "AF", "AF", "EU", "EU", "AF", "OC", "EU", 
  "AF", "AS", "AS", "AS", "OC", "NA", "AF", "NA", "EU", "AF", 
  "AS", "AF", "NA", "AS", "AF", "AF", "OC", "AF", "OC", "AF", 
  "NA", "EU", "EU", "AS", "OC", "OC", "OC", "AS", "NA", "SA", 
  "OC", "OC", "AS", "AS", "EU", "NA", "OC", "NA", "AS", "EU", 
  "OC", "SA", "AS", "AF", "EU", "EU", "AF", "AS", "OC", "AF", 
  "AF", "EU", "AS", "AF", "EU", "EU", "EU", "AF", "EU", "AF", 
  "AF", "SA", "AF", "NA", "AS", "AF", "NA", "AF", "AN", "AF", 
  "AS", "AS", "OC", "AS", "AF", "OC", "AS", "EU", "NA", "OC", 
  "AS", "AF", "EU", "AF", "OC", "NA", "SA", "AS", "EU", "NA", 
  "SA", "NA", "NA", "AS", "OC", "OC", "OC", "AS", "AF", "EU", 
  "AF", "AF", "EU", "AF", "--", "--", "--", "EU", "EU", "EU", 
  "EU", "NA", "NA"
);
    
}
function geoip_load_shared_mem ($file) {

  $fp = fopen($file, "rb");
  if (!$fp) {
    print "error opening $file: $php_errormsg\n";
    exit;
  }
  $s_array = fstat($fp);
  $size = $s_array['size'];
  if ($shmid = @shmop_open (GEOIP_SHM_KEY, "w", 0, 0)) {
    shmop_delete ($shmid);
    shmop_close ($shmid);
  }
  $shmid = shmop_open (GEOIP_SHM_KEY, "c", 0644, $size);
  shmop_write ($shmid, fread($fp, $size), 0);
  shmop_close ($shmid);
}

function _setup_segments($gi){
  $gi->databaseType = GEOIP_COUNTRY_EDITION;
  $gi->record_length = STANDARD_RECORD_LENGTH;
  if ($gi->flags & GEOIP_SHARED_MEMORY) {
    $offset = @shmop_size ($gi->shmid) - 3;
    for ($i = 0; $i < STRUCTURE_INFO_MAX_SIZE; $i++) {
        $delim = @shmop_read ($gi->shmid, $offset, 3);
        $offset += 3;
        if ($delim == (chr(255).chr(255).chr(255))) {
            $gi->databaseType = ord(@shmop_read ($gi->shmid, $offset, 1));
            $offset++;

            if ($gi->databaseType == GEOIP_REGION_EDITION_REV0){
                $gi->databaseSegments = GEOIP_STATE_BEGIN_REV0;
            } else if ($gi->databaseType == GEOIP_REGION_EDITION_REV1){
                $gi->databaseSegments = GEOIP_STATE_BEGIN_REV1;
	    } else if (($gi->databaseType == GEOIP_CITY_EDITION_REV0)||
                     ($gi->databaseType == GEOIP_CITY_EDITION_REV1) 
                    || ($gi->databaseType == GEOIP_ORG_EDITION)
                    || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
		    || ($gi->databaseType == GEOIP_ISP_EDITION)
		    || ($gi->databaseType == GEOIP_LOCATIONA_EDITION)
		    || ($gi->databaseType == GEOIP_ACCURACYRADIUS_EDITION)
		    || ($gi->databaseType == GEOIP_ASNUM_EDITION)){
                $gi->databaseSegments = 0;
                $buf = @shmop_read ($gi->shmid, $offset, SEGMENT_RECORD_LENGTH);
                for ($j = 0;$j < SEGMENT_RECORD_LENGTH;$j++){
                    $gi->databaseSegments += (ord($buf[$j]) << ($j * 8));
                }
	            if (($gi->databaseType == GEOIP_ORG_EDITION)
                        || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
			|| ($gi->databaseType == GEOIP_ISP_EDITION)) {
	                $gi->record_length = ORG_RECORD_LENGTH;
                }
            }
            break;
        } else {
            $offset -= 4;
        }
    }
    if (($gi->databaseType == GEOIP_COUNTRY_EDITION)||
        ($gi->databaseType == GEOIP_PROXY_EDITION)||
        ($gi->databaseType == GEOIP_NETSPEED_EDITION)){
        $gi->databaseSegments = GEOIP_COUNTRY_BEGIN;
    }
  } else {
    $filepos = ftell($gi->filehandle);
    fseek($gi->filehandle, -3, SEEK_END);
    for ($i = 0; $i < STRUCTURE_INFO_MAX_SIZE; $i++) {
        $delim = fread($gi->filehandle,3);
        if ($delim == (chr(255).chr(255).chr(255))){
        $gi->databaseType = ord(fread($gi->filehandle,1));
        if ($gi->databaseType == GEOIP_REGION_EDITION_REV0){
            $gi->databaseSegments = GEOIP_STATE_BEGIN_REV0;
        }
        else if ($gi->databaseType == GEOIP_REGION_EDITION_REV1){
	    $gi->databaseSegments = GEOIP_STATE_BEGIN_REV1;
                }  else if (($gi->databaseType == GEOIP_CITY_EDITION_REV0)
                    || ($gi->databaseType == GEOIP_CITY_EDITION_REV1) 
                    || ($gi->databaseType == GEOIP_ORG_EDITION) 
                    || ($gi->databaseType == GEOIP_DOMAIN_EDITION)
		    || ($gi->databaseType == GEOIP_ISP_EDITION)
		    || ($gi->databaseType == GEOIP_LOCATIONA_EDITION)
		    || ($gi->databaseType == GEOIP_ACCURACYRADIUS_EDITION)
                    || ($gi->databaseType == GEOIP_ASNUM_EDITION)){
            $gi->databaseSegments = 0;
            $buf = fread($gi->filehandle,SEGMENT_RECORD_LENGTH);
            for ($j = 0;$j < SEGMENT_RECORD_LENGTH;$j++){
            $gi->databaseSegments += (ord($buf[$j]) << ($j * 8));
            }
	    if (   ( $gi->databaseType == GEOIP_ORG_EDITION )
                || ( $gi->databaseType == GEOIP_DOMAIN_EDITION )
                || ( $gi->databaseType == GEOIP_ISP_EDITION ) ) {
	    $gi->record_length = ORG_RECORD_LENGTH;
            }
        }
        break;
        } else {
        fseek($gi->filehandle, -4, SEEK_CUR);
        }
    }
    if (($gi->databaseType == GEOIP_COUNTRY_EDITION)||
        ($gi->databaseType == GEOIP_PROXY_EDITION)||
        ($gi->databaseType == GEOIP_NETSPEED_EDITION)){
         $gi->databaseSegments = GEOIP_COUNTRY_BEGIN;
    }
    fseek($gi->filehandle,$filepos,SEEK_SET);
  }
  return $gi;
}

function geoip_open($filename, $flags) {
  $gi = new GeoIP;
  $gi->flags = $flags;
  if ($gi->flags & GEOIP_SHARED_MEMORY) {
    $gi->shmid = @shmop_open (GEOIP_SHM_KEY, "a", 0, 0);
    } else {
    $gi->filehandle = fopen($filename,"rb") or die( "Can not open $filename\n" );
    if ($gi->flags & GEOIP_MEMORY_CACHE) {
        $s_array = fstat($gi->filehandle);
        $gi->memory_buffer = fread($gi->filehandle, $s_array['size']);
    }
  }

  $gi = _setup_segments($gi);
  return $gi;
}

function geoip_close($gi) {
  if ($gi->flags & GEOIP_SHARED_MEMORY) {
    return true;
  }

  return fclose($gi->filehandle);
}

function geoip_country_id_by_name($gi, $name) {
  $addr = gethostbyname($name);
  if (!$addr || $addr == $name) {
    return false;
  }
  return geoip_country_id_by_addr($gi, $addr);
}

function geoip_country_code_by_name($gi, $name) {
  $country_id = geoip_country_id_by_name($gi,$name);
  if ($country_id !== false) {
        return $gi->GEOIP_COUNTRY_CODES[$country_id];
  }
  return false;
}

function geoip_country_name_by_name($gi, $name) {
  $country_id = geoip_country_id_by_name($gi,$name);
  if ($country_id !== false) {
        return $gi->GEOIP_COUNTRY_NAMES[$country_id];
  }
  return false;
}

function geoip_country_id_by_addr($gi, $addr) {
  $ipnum = ip2long($addr);
  return _geoip_seek_country($gi, $ipnum) - GEOIP_COUNTRY_BEGIN;
}

function geoip_country_code_by_addr($gi, $addr) {
  if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) {
    $record = geoip_record_by_addr($gi,$addr);
    if ( $record !== false ) {
      return $record->country_code;
    }
  } else {
    $country_id = geoip_country_id_by_addr($gi,$addr);
    if ($country_id !== false) {
      return $gi->GEOIP_COUNTRY_CODES[$country_id];
    }
  }
  return false;
}

function geoip_country_name_by_addr($gi, $addr) {
  if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) {
    $record = geoip_record_by_addr($gi,$addr);
    return $record->country_name;
  } else {
    $country_id = geoip_country_id_by_addr($gi,$addr);
    if ($country_id !== false) {
      return $gi->GEOIP_COUNTRY_NAMES[$country_id];
    }
  }
  return false;
}

function _geoip_seek_country($gi, $ipnum) {
  $offset = 0;
  for ($depth = 31; $depth >= 0; --$depth) {
    if ($gi->flags & GEOIP_MEMORY_CACHE) {
      // workaround php's broken substr, strpos, etc handling with
      // mbstring.func_overload and mbstring.internal_encoding
      $enc = mb_internal_encoding();
       mb_internal_encoding('ISO-8859-1'); 

      $buf = substr($gi->memory_buffer,
                            2 * $gi->record_length * $offset,
                            2 * $gi->record_length);

      mb_internal_encoding($enc);
    } elseif ($gi->flags & GEOIP_SHARED_MEMORY) {
      $buf = @shmop_read ($gi->shmid, 
                            2 * $gi->record_length * $offset,
                            2 * $gi->record_length );
        } else {
      fseek($gi->filehandle, 2 * $gi->record_length * $offset, SEEK_SET) == 0
        or die("fseek failed");
      $buf = fread($gi->filehandle, 2 * $gi->record_length);
    }
    $x = array(0,0);
    for ($i = 0; $i < 2; ++$i) {
      for ($j = 0; $j < $gi->record_length; ++$j) {
        $x[$i] += ord($buf[$gi->record_length * $i + $j]) << ($j * 8);
      }
    }
    if ($ipnum & (1 << $depth)) {
      if ($x[1] >= $gi->databaseSegments) {
        return $x[1];
      }
      $offset = $x[1];
        } else {
      if ($x[0] >= $gi->databaseSegments) {
        return $x[0];
      }
      $offset = $x[0];
    }
  }
  trigger_error("error traversing database - perhaps it is corrupt?", E_USER_ERROR);
  return false;
}

function _get_org($gi,$ipnum){
  $seek_org = _geoip_seek_country($gi,$ipnum);
  if ($seek_org == $gi->databaseSegments) {
    return NULL;
  }
  $record_pointer = $seek_org + (2 * $gi->record_length - 1) * $gi->databaseSegments;
  if ($gi->flags & GEOIP_SHARED_MEMORY) {
    $org_buf = @shmop_read ($gi->shmid, $record_pointer, MAX_ORG_RECORD_LENGTH);
    } else {
    fseek($gi->filehandle, $record_pointer, SEEK_SET);
    $org_buf = fread($gi->filehandle,MAX_ORG_RECORD_LENGTH);
  }
  // workaround php's broken substr, strpos, etc handling with
  // mbstring.func_overload and mbstring.internal_encoding
  $enc = mb_internal_encoding();
  mb_internal_encoding('ISO-8859-1'); 
  $org_buf = substr($org_buf, 0, strpos($org_buf, "\0"));
  mb_internal_encoding($enc);
  return $org_buf;
}

function geoip_org_by_addr ($gi,$addr) {
  if ($addr == NULL) {
    return 0;
  }
  $ipnum = ip2long($addr);
  return _get_org($gi, $ipnum);
}

function _get_region($gi,$ipnum){
  if ($gi->databaseType == GEOIP_REGION_EDITION_REV0){
    $seek_region = _geoip_seek_country($gi,$ipnum) - GEOIP_STATE_BEGIN_REV0;
    if ($seek_region >= 1000){
      $country_code = "US";
      $region = chr(($seek_region - 1000)/26 + 65) . chr(($seek_region - 1000)%26 + 65);
    } else {
            $country_code = $gi->GEOIP_COUNTRY_CODES[$seek_region];
      $region = "";
    }
  return array ($country_code,$region);
    }  else if ($gi->databaseType == GEOIP_REGION_EDITION_REV1) {
    $seek_region = _geoip_seek_country($gi,$ipnum) - GEOIP_STATE_BEGIN_REV1;
    //print $seek_region;
    if ($seek_region < US_OFFSET){
      $country_code = "";
      $region = "";  
        } else if ($seek_region < CANADA_OFFSET) {
      $country_code = "US";
      $region = chr(($seek_region - US_OFFSET)/26 + 65) . chr(($seek_region - US_OFFSET)%26 + 65);
        } else if ($seek_region < WORLD_OFFSET) {
      $country_code = "CA";
      $region = chr(($seek_region - CANADA_OFFSET)/26 + 65) . chr(($seek_region - CANADA_OFFSET)%26 + 65);
    } else {
            $country_code = $gi->GEOIP_COUNTRY_CODES[($seek_region - WORLD_OFFSET) / FIPS_RANGE];
      $region = "";
    }
  return array ($country_code,$region);
  }
}

function geoip_region_by_addr ($gi,$addr) {
  if ($addr == NULL) {
    return 0;
  }
  $ipnum = ip2long($addr);
  return _get_region($gi, $ipnum);
}

function getdnsattributes ($l,$ip){
  $r = new Net_DNS_Resolver();
  $r->nameservers = array("ws1.maxmind.com");
  $p = $r->search($l."." . $ip .".s.maxmind.com","TXT","IN");
  $str = is_object($p->answer[0])?$p->answer[0]->string():'';
  ereg("\"(.*)\"",$str,$regs);
  $str = $regs[1];
  return $str;
}
$area_config["Beijing"] = "北京市";
$area_config["Tianjin"] = "天津市";
$area_config["Shanghai"] = "上海市";
$area_config["Hefei"] = "合肥市";
$area_config["Anqing"] = "安庆市";
$area_config["Chaohu"] = "巢湖市";
$area_config["Chizhou"] = "池州市";
$area_config["Fuyang"] = "阜阳市";
$area_config["Huaibei"] = "淮北市";
$area_config["Liuan"] = "六安市";
$area_config["Maanshan"] = "马鞍山市";
$area_config["Suzhou1"] = "宿州市";
$area_config["Tongling"] = "铜陵市";
$area_config["Xuancheng"] = "宣城市";
$area_config["Fuzhou"] = "福州市";
$area_config["Nanping"] = "南平市";
$area_config["Ningde"] = "宁德市";
$area_config["Quanzhou"] = "泉州市";
$area_config["Sanming"] = "三明市";
$area_config["Zhangzhou"] = "漳州市";
$area_config["Lanzhou"] = "兰州市";
$area_config["Chengxian"] = "成县市";
$area_config["Dingxi"] = "定西市";
$area_config["Jiayuguan"] = "嘉峪关市";
$area_config["Jinchang"] = "金昌市";
$area_config["Linxia"] = "临夏回族自治州";
$area_config["Pingliang"] = "平凉市";
$area_config["Tianshui"] = "天水市";
$area_config["Wuwei"] = "武威市";
$area_config["Guangzhou"] = "广州市";
$area_config["Baoan"] = "宝安";
$area_config["Conghua"] = "从化市";
$area_config["Dongguan"] = "东莞市";
$area_config["Foshan"] = "佛山市";
$area_config["Gaoyao"] = "高要市";
$area_config["Heshan"] = "鹤山市";
$area_config["Heyuan"] = "河源市";
$area_config["Huizhou"] = "惠州市";
$area_config["Humen"] = "虎门";
$area_config["Jieyang"] = "揭阳市";
$area_config["Kaiping"] = "开平市";
$area_config["Leizhou"] = "雷州市";
$area_config["Lianjiang"] = "廉江市";
$area_config["Lufeng"] = "陆丰市";
$area_config["Luoding"] = "罗定市";
$area_config["Meizhou"] = "梅州市";
$area_config["Puning"] = "普宁市";
$area_config["Shantou"] = "汕头市";
$area_config["Shanwei"] = "汕尾市";
$area_config["Shenzhen"] = "深圳市";
$area_config["Shunde"] = "顺德市";
$area_config["Taishan"] = "台山市";
$area_config["Wuchuan"] = "吴川市";
$area_config["Yangjiang"] = "阳江市";
$area_config["Yingde"] = "英德市";
$area_config["Zengcheng"] = "增城市";
$area_config["Zhanjiang"] = "湛江市";
$area_config["Zhongshan"] = "中山市";
$area_config["Zhuhai"] = "珠海市";
$area_config["Baise"] = "百色市";
$area_config["Beihai"] = "北海市";
$area_config["Guigang"] = "贵港市";
$area_config["Guilin"] = "桂林市";
$area_config["Heshan1"] = "合山市";
$area_config["Hezhou"] = "贺州市";
$area_config["Qinzhou"] = "钦州市";
$area_config["Wuzhou"] = "梧州市";
$area_config["Guiyang"] = "贵阳市";
$area_config["Anshun"] = "安顺市";
$area_config["Liupanshui"] = "六盘水市";
$area_config["Qiannan"] = "黔南布依族苗族自治州";
$area_config["Qingzhen"] = "清镇市";
$area_config["Tongren"] = "铜仁地区";
$area_config["Haikou"] = "海口市";
$area_config["Sanya"] = "三亚市";
$area_config["Baoding"] = "保定市";
$area_config["Cangzhou"] = "沧州市";
$area_config["Handan"] = "邯郸市";
$area_config["Hejian"] = "河间市";
$area_config["Langfang"] = "廊坊市";
$area_config["Qianan"] = "迁安市";
$area_config["Tangshan"] = "唐山市";
$area_config["Wuan"] = "武安市";
$area_config["Zhangjiakou"] = "张家口市";
$area_config["Haerbin"] = "哈尔滨市";
$area_config["Hegang"] = "鹤岗市";
$area_config["Heihe"] = "黑河市";
$area_config["Jixi"] = "鸡西市";
$area_config["Mudanjiang"] = "牡丹江市";
$area_config["Qitaihe"] = "七台河市";
$area_config["Shuangyashan"] = "双鸭山市";
$area_config["Yichun"] = "伊春市";
$area_config["Zhengzhou"] = "郑州市";
$area_config["Gongyi"] = "巩义市";
$area_config["Hebi"] = "鹤壁市";
$area_config["Jiyuan"] = "济源市";
$area_config["Kaifeng"] = "开封市";
$area_config["Luoyang"] = "洛阳市";
$area_config["Nanyang"] = "南阳市";
$area_config["Puyang"] = "濮阳市";
$area_config["Sanmenxia"] = "三门峡市";
$area_config["Xinxiang"] = "新乡市";
$area_config["Xinyang"] = "信阳市";
$area_config["Zhoukou"] = "周口市";
$area_config["Zhumadian"] = "驻马店市";
$area_config["Jingmen"] = "荆门市";
$area_config["Shiyan"] = "十堰市";
$area_config["Xiangfan"] = "襄樊市";
$area_config["Xiaogan"] = "孝感市";
$area_config["Changsha"] = "长沙市";
$area_config["Chenzhou"] = "郴州市";
$area_config["Huaihua"] = "怀化市";
$area_config["Loudi"] = "娄底市";
$area_config["Xiangtan"] = "湘潭市";
$area_config["Xiangxi"] = "湘西土家族苗族自治州";
$area_config["Yongzhou"] = "永州市";
$area_config["Zhuzhou"] = "株洲市";
$area_config["Changshu"] = "常熟市";
$area_config["Changzhou"] = "常州市";
$area_config["Dongtai"] = "东台市";
$area_config["Gaoyou"] = "高邮市";
$area_config["Huaian"] = "淮安市";
$area_config["Jiangdu"] = "江都市";
$area_config["Jingjiang"] = "靖江市";
$area_config["Jintan"] = "金坛市";
$area_config["Lianyungang"] = "连云港市";
$area_config["Liyang"] = "溧阳市";
$area_config["Pizhou"] = "邳州市";
$area_config["Qidong"] = "启东市";
$area_config["Suqian"] = "宿迁市";
$area_config["Suzhou"] = "苏州市";
$area_config["Taixing"] = "泰兴市";
$area_config["Taizhou1"] = "泰州市";
$area_config["Wujiang"] = "吴江市";
$area_config["Wuxi"] = "无锡市";
$area_config["Xinyi"] = "新沂市";
$area_config["Xuzhou"] = "徐州市";
$area_config["Yangzhong"] = "扬中市";
$area_config["Yangzhou"] = "扬州市";
$area_config["Yizheng"] = "仪征市";
$area_config["Zhenjiang"] = "镇江市";
$area_config["Fuzhou1"] = "抚州市";
$area_config["Ganzhou"] = "赣州市";
$area_config["Shangrao"] = "上饶市";
$area_config["Xinyu"] = "新余市";
$area_config["Yingtan"] = "鹰潭市";
$area_config["Changchun"] = "长春市";
$area_config["Baishan"] = "白山市";
$area_config["Jian1"] = "集安市";
$area_config["Siping"] = "四平市";
$area_config["Tonghua"] = "通化市";
$area_config["Shenyang"] = "沈阳市";
$area_config["Anshan"] = "鞍山市";
$area_config["Chaoyang"] = "朝阳市";
$area_config["Dalian"] = "大连市";
$area_config["Fushun"] = "抚顺市";
$area_config["Fuxin"] = "阜新市";
$area_config["Jinzhou"] = "锦州市";
$area_config["Liaoyang"] = "辽阳市";
$area_config["Tieling"] = "铁岭市";
$area_config["Yingkou"] = "营口市";
$area_config["Alashan"] = "阿拉善盟";
$area_config["Baotou"] = "包头市";
$area_config["Chifeng"] = "赤峰市";
$area_config["Daxinganling"] = "大兴安岭地区";
$area_config["Hulunbeier"] = "呼伦贝尔市";
$area_config["Tongliao"] = "通辽市";
$area_config["Wulanchabu"] = "乌兰察布市";
$area_config["Xilinguole"] = "锡林郭勒盟";
$area_config["Yakeshi"] = "牙克石市";
$area_config["Yinchuan"] = "银川市";
$area_config["Shizuishan"] = "石嘴山市";
$area_config["Wuzhong"] = "吴忠市";
$area_config["Guoluo"] = "果洛藏族自治州";
$area_config["Haibei"] = "海北藏族自治州";
$area_config["Haixi"] = "海西蒙古族藏族自治州";
$area_config["Huangnan"] = "黄南藏族自治州";
$area_config["Jinan"] = "济南市";
$area_config["Binzhou"] = "滨州市";
$area_config["Dongying"] = "东营市";
$area_config["Heze"] = "荷泽市";
$area_config["Laiwu"] = "莱芜市";
$area_config["Liaocheng"] = "聊城市";
$area_config["Qingdao"] = "青岛市";
$area_config["Rizhao"] = "日照市";
$area_config["Weifang"] = "潍坊市";
$area_config["Weihai"] = "威海市";
$area_config["Zaozhuang"] = "枣庄市";
$area_config["Zibo"] = "淄博市";
$area_config["Ankang"] = "安康市";
$area_config["Baoji"] = "宝鸡市";
$area_config["Tongchuan"] = "铜川市";
$area_config["Weinan"] = "渭南市";
$area_config["Yanan"] = "延安市";
$area_config["Yulin"] = "榆林市";
$area_config["Changzhi"] = "长治市";
$area_config["Datong"] = "大同市";
$area_config["Jinzhong"] = "晋中市";
$area_config["Linfen"] = "临汾市";
$area_config["Xinzhou"] = "忻州市";
$area_config["Yangquan"] = "阳泉市";
$area_config["Chengdu"] = "成都市";
$area_config["Aba"] = "阿坝藏族羌族自治州";
$area_config["Dazhou"] = "达州市";
$area_config["Deyang"] = "德阳市";
$area_config["Liangshan"] = "凉山彝族自治州";
$area_config["Luzhou"] = "泸州市";
$area_config["Nanchong"] = "南充市";
$area_config["Panzhihua"] = "攀枝花市";
$area_config["Yibin"] = "宜宾市";
$area_config["Zigong"] = "自贡市";
$area_config["Changji"] = "昌吉回族自治州";
$area_config["Hami"] = "哈密地区";
$area_config["Miquan"] = "米泉市";
$area_config["Shihezi"] = "石河子市";
$area_config["Tulufan"] = "吐鲁番地区";
$area_config["Yili"] = "伊犁哈萨克自治州";
$area_config["Ali"] = "阿里地区";
$area_config["Changdu"] = "昌都地区";
$area_config["Rikaze"] = "日喀则地区";
$area_config["Shannan"] = "山南地区";
$area_config["Baoshan"] = "保山市";
$area_config["Chuxiong"] = "楚雄彝族自治州";
$area_config["Diqing"] = "迪庆藏族自治州";
$area_config["Honghe"] = "红河哈尼族彝族自治州";
$area_config["Lincang"] = "临沧市";
$area_config["Luxi"] = "潞西市";
$area_config["Qujing"] = "曲靖市";
$area_config["Simao"] = "思茅市";
$area_config["Xishuangbanna"] = "西双版纳傣族自治州";
$area_config["Yuxi"] = "玉溪市";
$area_config["Hangzhou"] = "杭州市";
$area_config["Fuyang1"] = "富阳市";
$area_config["Huzhou"] = "湖州市";
$area_config["Jiande"] = "建德市";
$area_config["Jinhua"] = "金华市";
$area_config["Linan"] = "临安市";
$area_config["Ningbo"] = "宁波市";
$area_config["Shaoxing"] = "绍兴市";
$area_config["Wenzhou"] = "温州市";
$area_config["Zhoushan"] = "舟山市";
$area_config["Chongqing"] = "重庆";
$area_config["Akesu"] = "阿克苏";
$area_config["Anning"] = "安宁";
$area_config["Anyang"] = "安阳";
$area_config["Baicheng"] = "白城";
$area_config["Baiyin"] = "白银";
$area_config["Bengbu"] = "蚌埠";
$area_config["Bazhong"] = "巴中";
$area_config["Benxi"] = "本溪";
$area_config["Bole"] = "博乐";
$area_config["Bozhou"] = "亳州";
$area_config["Changde"] = "常德";
$area_config["Chaozhou"] = "潮州";
$area_config["Chengde"] = "承德";
$area_config["Chenggu"] = "城固";
$area_config["Chibi"] = "赤壁";
$area_config["Chishui"] = "赤水";
$area_config["Chongzuo"] = "崇左";
$area_config["Chuzhou"] = "滁州";
$area_config["Cixi"] = "慈溪";
$area_config["Dali"] = "大理";
$area_config["Dandong"] = "丹东";
$area_config["Danyang"] = "丹阳";
$area_config["Daqing"] = "大庆";
$area_config["Dezhou"] = "德州";
$area_config["Dongyang"] = "东阳";
$area_config["Douyun"] = "都匀";
$area_config["Dunhua"] = "敦化";
$area_config["Eerduosi"] = "鄂尔多斯";
$area_config["Enshi"] = "恩施";
$area_config["Fangchenggang"] = "防城港";
$area_config["Feicheng"] = "肥城";
$area_config["Fenghua"] = "奉化";
$area_config["Ganyu"] = "赣榆";
$area_config["Gaoming"] = "高明";
$area_config["Geermu"] = "格尔木";
$area_config["Gejiu"] = "个旧";
$area_config["Guangan"] = "广安";
$area_config["Guangyuan"] = "广元";
$area_config["Gubaotou"] = "古包头";
$area_config["Guyuan"] = "固原";
$area_config["Haicheng"] = "海城";
$area_config["Haimen"] = "海门";
$area_config["Haining"] = "海宁";
$area_config["Hanzhong"] = "汉中";
$area_config["Hengshui"] = "衡水";
$area_config["Hengyang"] = "衡阳";
$area_config["Hetian"] = "和田";
$area_config["Huadou"] = "花都";
$area_config["Huainan"] = "淮南";
$area_config["Huanggang"] = "黄冈";
$area_config["Huangshan"] = "黄山";
$area_config["Huangshi"] = "黄石";
$area_config["Huhehaote"] = "呼和浩特";
$area_config["Huludao"] = "葫芦岛";
$area_config["Jiamusi"] = "佳木斯";
$area_config["Jian"] = "吉安";
$area_config["Jiangdou"] = "江都";
$area_config["Jiangmen"] = "江门";
$area_config["Jiangyin"] = "江阴";
$area_config["Jiaonan"] = "胶南";
$area_config["Jiaozhou"] = "胶州";
$area_config["Jiaozuo"] = "焦作";
$area_config["Jiashan"] = "嘉善";
$area_config["Jiaxing"] = "嘉兴";
$area_config["Jiexiu"] = "介休";
$area_config["Jilin"] = "吉林";
$area_config["Jimo"] = "即墨";
$area_config["Jincheng"] = "晋城";
$area_config["Jingdezhen"] = "景德镇";
$area_config["Jinghong"] = "景洪";
$area_config["Jingzhou"] = "荆州";
$area_config["Jining1"] = "集宁";
$area_config["Jining"] = "济宁";
$area_config["Jinjiang"] = "晋江";
$area_config["Jishou"] = "吉首";
$area_config["Jiujiang"] = "九江";
$area_config["Jiuquan"] = "酒泉";
$area_config["Jurong"] = "句容";
$area_config["Kaili"] = "凯里";
$area_config["Kaiyuan"] = "开远";
$area_config["Kashen"] = "喀什";
$area_config["Kelamayi"] = "克拉玛依";
$area_config["Kuerle"] = "库尔勒";
$area_config["Kuitun"] = "奎屯";
$area_config["Kunming"] = "昆明";
$area_config["Kunshan"] = "昆山";
$area_config["Laibin"] = "来宾";
$area_config["Laixi"] = "莱西";
$area_config["Laizhou"] = "莱州";
$area_config["Lasa"] = "拉萨";
$area_config["Leshan"] = "乐山";
$area_config["Liaoyuan"] = "辽源";
$area_config["Lijiang"] = "丽江";
$area_config["Lingbao"] = "灵宝";
$area_config["Linhe"] = "临河";
$area_config["Linyi"] = "临沂";
$area_config["Lishui"] = "丽水";
$area_config["Liuzhou"] = "柳州";
$area_config["Longhai"] = "龙海";
$area_config["Longyan"] = "龙岩";
$area_config["Luohe"] = "漯河";
$area_config["Lvliang"] = "吕梁";
$area_config["Lvshun"] = "旅顺";
$area_config["Maoming"] = "茂名";
$area_config["Meihekou"] = "梅河口";
$area_config["Meishan"] = "眉山";
$area_config["Mianxian"] = "勉县";
$area_config["Mianyang"] = "绵阳";
$area_config["Nanan"] = "南安";
$area_config["Nanchang"] = "南昌";
$area_config["Nanjing"] = "南京";
$area_config["Nanning"] = "南宁";
$area_config["Nantong"] = "南通";
$area_config["Neijiang"] = "内江";
$area_config["Panjin"] = "盘锦";
$area_config["Penglai"] = "蓬莱";
$area_config["Pingdingshan"] = "平顶山";
$area_config["Pingdu"] = "平度";
$area_config["Pinghu"] = "平湖";
$area_config["Pingxiang"] = "萍乡";
$area_config["Pulandian"] = "普兰店";
$area_config["Putian"] = "莆田";
$area_config["Qingyang"] = "庆阳";
$area_config["Qingyuan"] = "清远";
$area_config["Qingzhou"] = "青州";
$area_config["Qinhuangdao"] = "秦皇岛";
$area_config["Qionghai"] = "琼海";
$area_config["Qiqihaer"] = "齐齐哈尔";
$area_config["Quzhou"] = "衢州";
$area_config["Rongcheng"] = "荣成";
$area_config["Rugao"] = "如皋";
$area_config["Ruian"] = "瑞安";
$area_config["Rushan"] = "乳山";
$area_config["Xiamen"] = "厦门";
$area_config["Shan"] = "佛山";
$area_config["Shangluo"] = "商洛";
$area_config["Shangqiu"] = "商丘";
$area_config["Shangyu"] = "上虞";
$area_config["Shaoguan"] = "韶关";
$area_config["Shaoyang"] = "邵阳";
$area_config["Shijiazhuang"] = "石家庄";
$area_config["Shilin"] = "石林";
$area_config["Shishi"] = "石狮";
$area_config["Shouguang"] = "寿光";
$area_config["Shuozhou"] = "朔州";
$area_config["Shuyang"] = "沭阳";
$area_config["Songyuan"] = "松原";
$area_config["Suining"] = "遂宁";
$area_config["Suizhou"] = "随州";
$area_config["Tacheng"] = "塔城";
$area_config["Taian"] = "泰安";
$area_config["Taicang"] = "太仓";
$area_config["Taiyuan"] = "太原";
$area_config["Taizhou"] = "泰州";
$area_config["Tengchong"] = "腾冲";
$area_config["Tengzhou"] = "滕州";
$area_config["Tianmen"] = "天门";
$area_config["Tonglu"] = "桐庐";
$area_config["Tongxiang"] = "桐乡";
$area_config["Tongzhou"] = "通州";
$area_config["Wafangdian"] = "瓦房店";
$area_config["Wendeng"] = "文登";
$area_config["Wenling"] = "温岭";
$area_config["Wuhai"] = "乌海";
$area_config["Wuhan"] = "武汉";
$area_config["Wuhu"] = "芜湖";
$area_config["Wulanhaote"] = "乌兰浩特";
$area_config["Xian"] = "西安";
$area_config["Xiangcheng"] = "项城";
$area_config["Xianggelila"] = "香格里拉";
$area_config["Xiangshan"] = "象山";
$area_config["Xiangxiang"] = "湘乡";
$area_config["Xianning"] = "咸宁";
$area_config["Xiantao"] = "仙桃";
$area_config["Xianyang"] = "咸阳";
$area_config["Xicang"] = "西藏";
$area_config["Xichang"] = "西昌";
$area_config["Xingtai"] = "邢台";
$area_config["Xingyi"] = "兴义";
$area_config["Xining"] = "西宁";
$area_config["Suyu"] = "宿豫";
$area_config["Xuchang"] = "许昌";
$area_config["Yaan"] = "雅安";
$area_config["Yanbian"] = "延边";
$area_config["Yancheng"] = "盐城";
$area_config["Yanji"] = "延吉";
$area_config["Yantai"] = "烟台";
$area_config["Yanzhou"] = "兖州";
$area_config["Yichang"] = "宜昌";
$area_config["Yichun1"] = "伊春";
$area_config["Yining"] = "伊宁";
$area_config["Yiwu"] = "义乌";
$area_config["Yixing"] = "宜兴";
$area_config["Yiyang"] = "益阳";
$area_config["Yongkang"] = "永康";
$area_config["Yueyang"] = "岳阳";
$area_config["Yuhuan"] = "玉环";
$area_config["Yulin1"] = "榆林";
$area_config["Yuncheng"] = "运城";
$area_config["Yuyao"] = "余姚";
$area_config["Changhai"] = "长海";
$area_config["Zhangjiagang"] = "张家港";
$area_config["Zhangjiajie"] = "张家界";
$area_config["Changle"] = "长乐";
$area_config["Zhangqiu"] = "章丘";
$area_config["Zhangye"] = "张掖";
$area_config["Zhaodong"] = "肇东";
$area_config["Zhaoqing"] = "肇庆";
$area_config["Zhaotong"] = "昭通";
$area_config["Zhucheng"] = "诸城";
$area_config["Zhuji"] = "诸暨";
$area_config["Zunyi"] = "遵义";
$area_config["Wulumuqi"] = "乌鲁木齐";
$area_config["Fuqing"] = "福清";
$area_config["Ezhou"] = "鄂州";
$area_config["Xiaoshan"] = "萧山";
$area_config["Xuanhua"] = "宣化";
$area_config["Jiangyou"] = "江油";
$area_config["Ziyang"] = "资阳";
$area_config["Xinji"] = "辛集";
$area_config["Wanzhou"] = "万州";
$area_config["Zoucheng"] = "邹城";
$area_config["Shaowu"] = "邵武";
$area_config["Jiangyan"] = "姜堰";
$area_config["Xiangyin"] = "湘阴";
$area_config["Songjiang"] = "松江";
$area_config["Liling"] = "醴陵";
$area_config["Fuling"] = "涪陵";
$area_config["Gongzhuling"] = "公主岭";
$area_config["Shexian"] = "歙县";
$area_config["Xinghua"] = "兴化";
?>
